
from flask import Flask,request,session,url_for,g,render_template,current_app
from utils import log_a,log_b,log_c
app = Flask(__name__)
app.config['SECRET_KEY'] = 'qwertyuio12='

# with app.app_context(): #这一步其实就是将上下文手动放到栈中
#     print(current_app.name)

@app.route('/')
def hello_world():
    #print(current_app.name) #current_app 表示当前的app
    #print(url_for('my_list')) #只有放到栈中才可以获取到/list/
    # username = request.args.get('username')
    # g.username = username
    # log_a(username)
    # log_b(username)
    # log_c(username)
    # log_a()
    # log_b()
    # log_c()
    a = 1
    b = 0
    c = a/b
    print(c)
    return render_template('index.html')
@app.before_first_request
def first_request():
    print("第一次请求要执行我 只是第一次")

@app.before_request
def before_request():
    user_id = session.get('user_id')
    if user_id:
        g.user = "kangbazi"

@app.context_processor
def context_processor():
    if hasattr(g,'user'):
        return {"current_user":g.user}

@app.route('/list/')
def my_list(): #通过函数名 反转获取 路由的名字
    session['user_id'] = 666
    #print(current_app.name) #current_app 表示当前的app
    return render_template('list.html')
# with app.app_context(): #这个依旧报错  因为这是应用上下文
    #应该放到请求上下文栈中
with app.test_request_context():
    print(url_for('my_list')) #这样就返回/list/

@app.errorhandler(404)
def page_not_found(error):
    return render_template('404.html')



@app.errorhandler(500)
def server_error(error):
    return render_template('500.html')

@app.errorhandler(400)
def args_error(error):
    return '请求参数不正确'

if __name__ == '__main__':
    app.run()
